home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5859 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  69 lines

  1. Newsgroups: comp.lang.c
  2. Path: tank.news.pipex.net!pipex!warwick!bsmail!talisker!nathan
  3. From: nathan@pact.srf.ac.uk (Nathan Sidwell)
  4. Subject: Re: Speed question here...
  5. Message-ID: <Dn341w.5K3@uns.bris.ac.uk>
  6. Followup-To: comp.lang.c
  7. Sender: usenet@uns.bris.ac.uk (Usenet news owner)
  8. Nntp-Posting-Host: talisker.pact.srf.ac.uk
  9. Organization: Inmos
  10. X-Newsreader: TIN [version 1.2 PL2]
  11. References: <4ftluh$1gkv@hearst.cac.psu.edu> <4g9rbg$3cl@ooze.val.net> <xduenrq418t.fsf@korppi.cs.tut.fi>
  12. Distribution: inet
  13. Date: Tue, 20 Feb 1996 17:17:56 GMT
  14.  
  15. Tero Pulkkinen (p150650@korppi.cs.tut.fi) wrote:
  16. :    >I was curious as to how fast something like the 
  17. :    >following would execute:
  18.  
  19. :    >    int x;
  20. :    >    node *ptr;   ptr in linked list
  21.  
  22. :    >       for ( ptr = first_node; ptr != NULL; ptr = ptr.next ) {
  23. :    >       for ( x = 0; x < max; x++ ) {
  24. :    >        array[x] = array_other[x];
  25. :    >           }
  26. :    >      } 
  27.  
  28. : You should be interested in the assignment, coz its the slowest part of that
  29. : code. :) If the array item happens to be something else than sizeof 2^n then
  30. : that assignment requires at least one multiply. :) Slow as hell. better
  31. : do it this way:
  32.  
  33. not necessarily true. the compiler could strength reduce a
  34. multiply by 5 (say) to ((a << 2) + a) for instance. OR the
  35. compiler could spot that an array is being stepped down, and use
  36. it's own pointer and increment that. OR it could spot an application
  37. of memcpy (if it could determine array and array_other are disjoint)
  38.  
  39. : And btw, if you want fast code, you should use do { } while() -construct
  40. : instead of for(;;) -loop. That way you avoid having two jumps inside the
  41. : loop. And if you have situation where you always dont have to execute the
  42. : insides of the loop, then can do it with an if... Most compilers put the
  43. : loop test to the start of the loop block, and then do unconditional jump
  44. : to the start of it. That's one too much. :)
  45. Compilers generally convert all loop source forms into a 
  46. generic one. In particular for loops are often compiled as,
  47.  
  48. <init code>
  49. goto test:
  50. label:
  51. <body code>
  52. <increment code>
  53. test:
  54. <test_code>
  55. jump<cond> label
  56.  
  57. In a do loop there will be no 'goto test'.
  58. If the compiler sees that the initialization code sets the conditions
  59. such that there is at least one loop iteration, it could delete the
  60. 'goto test' too (I have no idea what the state of the art is on this
  61. one though).
  62.  
  63. nathan
  64. --
  65. Nathan Sidwell                         Holder of the Xmris home page
  66. Chameleon Architecture Group at SGS-Thomson, formerly Inmos
  67. http://www.pact.srf.ac.uk/~nathan/                  Tel 0117 9707182
  68. nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
  69.